package com.ruoyi.project.module.errorcode.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.module.errorcode.domain.Errorcode;
import com.ruoyi.project.module.errorcode.service.IErrorcodeService;
import com.ruoyi.project.module.project.domain.Project;
import com.ruoyi.project.module.project.service.IProjectService;
import com.ruoyi.project.module.utils.ProjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 错误代码 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-10-08
 */
@Controller
@RequestMapping("/module/errorcode")
public class ErrorcodeController extends BaseController
{

	Logger logger=LoggerFactory.getLogger(ErrorcodeController.class);

    private String prefix = "module/errorcode";
	
	@Autowired
	private IErrorcodeService errorcodeService;

	@Autowired
	private IProjectService projectService;

	@RequiresPermissions("module:errorcode:view")
	@GetMapping()
	public String errorcode()
	{
	    return prefix + "/errorcode";
	}
	
	/**
	 * 查询错误代码列表
	 */
	@RequiresPermissions("module:errorcode:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Errorcode errorcode)
	{
		startPage();
		logger.debug("---------searchValue={}",errorcode.getSearchValue());
		logger.debug("---------params={}",JSONObject.toJSONString(errorcode.getParams()));
        List<Errorcode> list = errorcodeService.selectErrorcodeList(errorcode);

		return getDataTable(list);
	}
	
	/**
	 * 新增错误代码
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Integer parentId, ModelMap mmap )
	{
		Project info = projectService.selectProjectById(parentId);
		mmap.put("project", info);
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存错误代码
	 */
	@RequiresPermissions("module:errorcode:add")
	@Log(title = "错误代码", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Errorcode errorcode)
	{
		System.out.println("errorcode="+errorcode.getErrorcode());
		return toAjax(errorcodeService.insertErrorcode(errorcode));
	}

	/**
	 * 修改错误代码
	 */
	@GetMapping("/edit/{apiErrorcodeId}")
	public String edit(@PathVariable("apiErrorcodeId") Integer apiErrorcodeId, ModelMap mmap)
	{
		Errorcode errorcode = errorcodeService.selectErrorcodeById(apiErrorcodeId);
		mmap.put("errorcode", errorcode);
		Project info = projectService.selectProjectById(errorcode.getApiProjectnameId());
		mmap.put("project", info);
		Integer errorGroupByProject = ProjectUtils.getErrorGroupByProject(projectService, info);
		mmap.put("errorGroup", errorGroupByProject);


		return prefix + "/edit";
	}
	
	/**
	 * 修改保存错误代码
	 */
	@RequiresPermissions("module:errorcode:edit")
	@Log(title = "错误代码", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Errorcode errorcode)
	{		
		return toAjax(errorcodeService.updateErrorcode(errorcode));
	}
	
	/**
	 * 删除错误代码
	 */
	@RequiresPermissions("module:errorcode:remove")
	@Log(title = "错误代码", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(errorcodeService.deleteErrorcodeByIds(ids));
	}
	
}
